package util;


import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

/**
 * @Description RestTemplate 工具类
 * @Author ruofei
 * @Date 2020/10/9 14:17
 * @Version
 **/
public class RestTemplateUtil {

    private static RestTemplate restTemplate;

    private RestTemplateUtil() {
    }

    public synchronized static RestTemplate getInstance() {
        if (restTemplate == null) {
            SSL factory = new SSL();
            factory.setReadTimeout(5000);
            factory.setConnectTimeout(15000);
            restTemplate = new RestTemplate(factory);
        }
        return restTemplate;
    }

    /**
     * post 请求
     * @param url 请求路径
     * @param data body数据(json格式)
     * @return
     */
    public static String post(String url, String data) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", "application/json");
        headers.add("Content-Encoding", "UTF-8");
        headers.add("Content-Type", "application/json; charset=UTF-8");
        HttpEntity<String> requestEntity = new HttpEntity<>(data, headers);
        return RestTemplateUtil.getInstance().postForObject(url, requestEntity, String.class);
    }

    /**
     * post 请求
     *
     * @param url        请求路径
     * @param data       body数据(json格式)
     * @param addHeaders 需要追加的header
     * @return
     */
    public static String post(String url, String data, Map<String, String> addHeaders) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", "application/json");
        headers.add("Content-Encoding", "UTF-8");
        headers.add("Content-Type", "application/json; charset=UTF-8");
        if (!CollectionUtils.isEmpty(addHeaders)) {
            for (String key : addHeaders.keySet()) {
                headers.add(key, addHeaders.get(key));
            }
        }
        HttpEntity<String> requestEntity = new HttpEntity<>(data, headers);
        return RestTemplateUtil.getInstance().postForObject(url, requestEntity, String.class);
    }

    /**
     * post 请求
     *
     * @param url        请求路径
     * @param
     * @param addHeaders 需要追加的header
     * @return
     */
    public static String post(String url, Map<String, Object> paramsMap, Map<String, String> addHeaders) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", "application/json");
        headers.add("Content-Encoding", "UTF-8");
        headers.add("Content-Type", "application/json");
        if (!CollectionUtils.isEmpty(addHeaders)) {
            for (String key : addHeaders.keySet()) {
                headers.add(key, addHeaders.get(key));
            }
        }
        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(paramsMap, headers);
        return RestTemplateUtil.getInstance().postForObject(url, requestEntity, String.class);
    }

    /**
     * get 请求
     * @param url 请求路径
     * @return
     */
    public static  String get(String url) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Accept", "application/json");
        headers.add("Content-Encoding", "UTF-8");
        headers.add("Content-Type", "application/json; charset=UTF-8");
        HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
        RestTemplate instance = RestTemplateUtil.getInstance();
        ResponseEntity<String> response = instance.exchange(url, HttpMethod.GET, requestEntity, String.class);
        return response.getBody();
    }

}
